<template>
  <div class="announcements-container">
    <div class="page-header">
      <h3>公告查看</h3>
      <el-input
        v-model="searchQuery"
        placeholder="搜索公告"
        style="width: 200px"
        @input="handleSearch">
        <i slot="prefix" class="el-icon-search"></i>
      </el-input>
    </div>

    <el-table :data="filteredAnnouncements" style="width: 100%" v-loading="loading">
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="content" label="内容" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="createTime" label="发布时间" width="180"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="公告详情" :visible.sync="dialogVisible" width="50%">
      <h4>{{ currentAnnouncement.title }}</h4>
      <p class="announcement-time">发布时间：{{ currentAnnouncement.createTime }}</p>
      <div class="announcement-content">{{ currentAnnouncement.content }}</div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'StudentAnnouncements',
  data() {
    return {
      loading: false,
      announcements: [],
      searchQuery: '',
      dialogVisible: false,
      currentAnnouncement: {}
    }
  },
  computed: {
    filteredAnnouncements() {
      if (!this.searchQuery) {
        return this.announcements
      }
      const query = this.searchQuery.toLowerCase()
      return this.announcements.filter(item => 
        item.title.toLowerCase().includes(query) ||
        item.content.toLowerCase().includes(query)
      )
    }
  },
  created() {
    this.getAnnouncements()
  },
  methods: {
    getAnnouncements() {
      this.loading = true
      // TODO: 调用获取公告列表接口
      setTimeout(() => {
        this.announcements = [
          {
            id: 1,
            title: '关于期末作业提交的通知',
            content: '各位同学：请于下周五之前提交期末作业...',
            createTime: '2024-03-16 10:00:00'
          }
        ]
        this.loading = false
      }, 1000)
    },
    handleSearch() {
      // 实时搜索，不需要额外处理，computed属性会自动过滤
    },
    handleView(row) {
      this.currentAnnouncement = row
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.announcements-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h3 {
  margin: 0;
}

.announcement-time {
  color: #909399;
  font-size: 14px;
  margin: 10px 0;
}

.announcement-content {
  line-height: 1.6;
  white-space: pre-wrap;
}
</style> 